var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import "./swiper.uniapp-sfc.css";
import {
  defineComponent,
  ref,
  watch,
  onMounted,
  getCurrentInstance,
  computed
} from "vue";
import FanAspectRatio from "../aspect-ratio/aspect-ratio.js";
import FanContactButton from "../contact-button/contact-button.js";
import FanImage from "../image/image.js";
import {
  createNamespace,
  createUniqueId,
  getElementBounding,
  transformStyleSize
} from "../utils";
import { swiperProps, swiperEmits, useSwiperState } from "./helper";
const __vue_sfc__ = defineComponent({
  name: createNamespace("Swiper")[0],
  options: {
    // https://uniapp.dcloud.io/matter.html#mp
    virtualHost: true
  },
  components: {
    FanAspectRatio,
    FanContactButton,
    FanImage
  },
  props: swiperProps,
  emits: swiperEmits,
  setup(props, context) {
    const uniqueId = createUniqueId({ prefix: "fanSwiper" });
    const innerCurrent = ref(props.current);
    watch(
      () => props.current,
      (val) => {
        if (val !== innerCurrent.value)
          innerCurrent.value = val;
      }
    );
    watch(innerCurrent, (val, prevVal) => {
      if (val === prevVal)
        return;
      context.emit("update:current", val);
      context.emit("change", val);
      if (props.autoHeight) {
        if (itemRects[val])
          currentHeight.value = itemRects[val].height;
        updateAutoHeight();
      }
    });
    const { swiperWidth, swiperAspectRatio } = useSwiperState(props);
    const _this = getCurrentInstance();
    const getSwiperWidth = () => __async(this, null, function* () {
      const { type } = props;
      if (type !== 2)
        return;
      const rect = yield getElementBounding({
        selector: "#" + uniqueId,
        scope: _this
      });
      if (!rect || !rect.watch)
        return;
      swiperWidth.value = rect.width;
    });
    onMounted(getSwiperWidth);
    watch(() => props.customStyle, getSwiperWidth);
    let autoHeightTimer;
    let itemRects = [];
    const currentHeight = ref(0);
    const theStyle = computed(() => {
      const s = transformStyleSize(props.customStyle);
      if (!props.autoHeight)
        return s;
      return `${s}height:${currentHeight.value}px;`;
    });
    const updateAutoHeight = () => {
      if (autoHeightTimer)
        clearTimeout(autoHeightTimer);
      const i = innerCurrent.value;
      setTimeout(() => __async(this, null, function* () {
        var _a;
        const rect = yield getElementBounding({
          selector: ".fan-swiper__item--active",
          scope: _this
        });
        if ((rect == null ? void 0 : rect.height) !== ((_a = itemRects[i]) == null ? void 0 : _a.height)) {
          if (rect.height)
            itemRects[i] = rect;
          autoHeightTimer = setTimeout(updateAutoHeight, 150);
        } else {
          currentHeight.value = rect == null ? void 0 : rect.height;
        }
      }), 0);
    };
    onMounted(() => {
      if (props.autoHeight)
        updateAutoHeight();
    });
    const onClick = (e, i) => {
      context.emit("click", e, i);
    };
    const showSlot = computed(() => {
      return props.customItem || context.slots.default;
    });
    const onChange = (e) => {
      var _a;
      const source = (_a = e.detail) == null ? void 0 : _a.source;
      if (source == "autoplay" || source == "touch") {
        innerCurrent.value = e.detail.current;
      }
    };
    return {
      uniqueId,
      theStyle,
      innerCurrent,
      swiperAspectRatio,
      showSlot,
      onClick,
      onChange
    };
  }
});
import { renderList as _renderList, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock, renderSlot as _renderSlot, createCommentVNode as _createCommentVNode, resolveComponent as _resolveComponent, createBlock as _createBlock, normalizeClass as _normalizeClass, normalizeStyle as _normalizeStyle, createElementVNode as _createElementVNode, withCtx as _withCtx, createVNode as _createVNode } from "vue";
function __vue_render__(_ctx, _cache) {
  const _component_FanImage = _resolveComponent("FanImage");
  const _component_FanContactButton = _resolveComponent("FanContactButton");
  const _component_swiper_item = _resolveComponent("swiper-item");
  const _component_swiper = _resolveComponent("swiper");
  const _component_FanAspectRatio = _resolveComponent("FanAspectRatio");
  return _ctx.list && _ctx.list.length ? (_openBlock(), _createBlock(_component_FanAspectRatio, {
    key: 0,
    style: _normalizeStyle(_ctx.theStyle),
    "aspect-ratio": _ctx.swiperAspectRatio
  }, {
    default: _withCtx(() => [
      _createVNode(_component_swiper, {
        id: _ctx.uniqueId,
        style: { "height": "100%", "max-height": "100%" },
        "adjust-height": "none",
        "indicator-dots": _ctx.list.length > 1 && _ctx.indicator,
        "indicator-color": "#f6f6f6",
        "indicator-active-color": _ctx.indicatorActiveColor,
        autoplay: !!_ctx.autoplay,
        interval: _ctx.autoplay,
        circular: _ctx.loop,
        duration: _ctx.duration,
        current: _ctx.innerCurrent,
        vertical: _ctx.vertical,
        "previous-margin": _ctx.type === 2 ? "38px" : "0",
        "next-margin": _ctx.type === 2 ? "38px" : "0",
        onChange: _ctx.onChange
      }, {
        default: _withCtx(() => [
          (_openBlock(true), _createElementBlock(
            _Fragment,
            null,
            _renderList(_ctx.list, (item, index) => {
              return _openBlock(), _createBlock(
                _component_swiper_item,
                { key: index },
                {
                  default: _withCtx(() => [
                    _createElementVNode(
                      "view",
                      {
                        style: _normalizeStyle({ height: _ctx.autoHeight ? "auto" : "100%" }),
                        class: _normalizeClass(["fan-swiper__item", {
                          "fan-swiper__item--type2": _ctx.type === 2,
                          "fan-swiper__item--active": _ctx.innerCurrent === index
                        }])
                      },
                      [
                        _ctx.showSlot ? _renderSlot(_ctx.$slots, "default", {
                          key: 0,
                          item,
                          index,
                          active: index === _ctx.innerCurrent
                        }) : (_openBlock(), _createBlock(_component_FanImage, {
                          key: 1,
                          "show-menu-by-longpress": _ctx.showMenuByLongpress,
                          src: item.cover,
                          mode: _ctx.autoHeight ? "widthFix" : "aspectFill",
                          "aspect-ratio": _ctx.autoHeight ? 0 : _ctx.aspectRatio,
                          radius: _ctx.innerRadius,
                          onClick: ($event) => _ctx.onClick(item, index)
                        }, null, 8, ["show-menu-by-longpress", "src", "mode", "aspect-ratio", "radius", "onClick"])),
                        item.link && item.link.mpContact ? (_openBlock(), _createBlock(_component_FanContactButton, {
                          key: 2,
                          full: true,
                          "mp-contact": item.link.mpContact
                        }, null, 8, ["mp-contact"])) : _createCommentVNode("v-if", true)
                      ],
                      6
                      /* CLASS, STYLE */
                    )
                  ]),
                  _: 2
                  /* DYNAMIC */
                },
                1024
                /* DYNAMIC_SLOTS */
              );
            }),
            128
            /* KEYED_FRAGMENT */
          ))
        ]),
        _: 3
        /* FORWARDED */
      }, 8, ["id", "indicator-dots", "indicator-active-color", "autoplay", "interval", "circular", "duration", "current", "vertical", "previous-margin", "next-margin", "onChange"])
    ]),
    _: 3
    /* FORWARDED */
  }, 8, ["style", "aspect-ratio"])) : _createCommentVNode("v-if", true);
}
__vue_sfc__.render = __vue_render__;
var stdin_default = __vue_sfc__;
export {
  stdin_default as default
};
